package model.code.line;

import model.caret.TextCaret;

/**
 * A representation of a syntax error within a given line
 * @author Neil Dickson
 */
public class SyntaxError {
	private	Line	line;
	private	int		startCol;
	private	int		endCol;
	private	String	description;
	private	boolean	hasEmphasis;

	/**
	 * Creates a new SyntaxError that is to have emphasis
	 * @param line	the line containing the error
	 * @param start	the starting column
	 * @param end	the ending column
	 * @param descr	a description of the error
	 */
	public SyntaxError(Line line, int start, int end, String descr) {
		this.line = line;
		startCol = start;
		endCol = end;
		description = descr;
		hasEmphasis = true;
	}

	/**
	 * Creates a new SyntaxError
	 * @param line			the line containing the error
	 * @param start			the starting column
	 * @param end			the ending column
	 * @param descr			a description of the error
	 * @param hasEmphasis	true if the SyntaxError is to have emphasis
	 */
	public SyntaxError(Line line,int start,int end,String descr,boolean hasEmphasis) {
		this.line = line;
		startCol = start;
		endCol = end;
		description = descr;
		this.hasEmphasis = hasEmphasis;
	}

	public int getStartCol() {
		return startCol;
	}

	public int getEndCol() {
		return endCol;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public boolean hasEmphasis() {
		return hasEmphasis;
	}

	public Line getLine() {
		return line;
	}

	public void setLine(Line line) {
		this.line = line;
		startCol = Math.min(line.length(),startCol);
		endCol = Math.min(line.length(),endCol);
	}

	public void setLocation(int start, int end) {
		startCol = start;
		endCol = end;
	}

	/**
	 * @return a {@link TextCaret} corresponding with the location of this error
	 */
	public TextCaret getCaret() {
		if (line==null) {
			return null;
		}
		LineList list = line.getList();
		int lineNum = (list!=null) ? list.indexOf(line) : -1;
		return new TextCaret(null,list,lineNum,startCol,lineNum,endCol,0);
	}

	public String toString() {
		return "Syntax Error"+((line!=null && line.getList()!=null && line.getList().getParent()!=null && line.getList().getParent().getFile()!=null)?" (in "+line.getList().getParent().getFile().getName()+","+line.getList().getParent().getName()+","+line.getList().indexOf(line)+")":"")+": "+description;
	}
}
